// pages/home/cultural/detail/detail.js
var QQMapWX = require("../../../../libs/qqmap-wx-jssdk1.2/qqmap-wx-jssdk")
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //from
    from: '',
    to: '',
    latitude: '',
    longitude: '',
    name: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'UJKBZ-JCQ63-OCQ3G-ONSE4-RHAS7-BAFWW'
    });
    //保存This指向
    var that = this
    // 获取可用区域宽高
    wx.getSystemInfo({
      success(res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    })
    //获取用户当前位置
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        //保存当前坐标
        that.data.latitude = latitude
        that.data.longitude = longitude
        //起始地址(当前获取到的用户初始坐标)
        that.data.from = that.data.latitude + "," + that.data.longitude
        console.log("用户起始坐标:", that.data.from)
      }
    })
    //逻辑处理部分代码
    var detailData = JSON.parse(decodeURIComponent(options.detailData))
    console.log(detailData)
    //目标地址(父组件传入的目标地址)
    that.data.to = detailData.location.lat + "," + detailData.location.lng
    console.log("用户终点坐标:", that.data.to)
    //目标地址名称
    that.data.name = detailData.title
    //响应式设置数据
    that.setData({
      detailData: detailData
    })
  },
  onChoose(event) {
    // console.log(event.currentTarget.dataset.getimage)
    var imgurl = event.currentTarget.dataset.getimage
    wx.previewImage({
      urls: [imgurl],
      showmenu: true
    })
  },
  formSubmit(e) {
    console.log("From:", this.data.from)
    console.log("To:", this.data.to)
    //保存this指向
    var _this = this;
    //调用距离计算接口
    qqmapsdk.direction({
      mode: 'transit', //'transit'(公交路线规划)、driving驾车、walking步行、bicycling骑行、ebicycling电动车
      sig: 'KnAAOQeoZtHBrTiT9PmUQi4AsdtDeQoW',
      from: _this.data.from,
      to: _this.data.to,
      success: function (res) {
        console.log(res);
        var ret = res.result.routes[0];
        var count = ret.steps.length;
        var pl = [];
        var coors = [];
        //获取各个步骤的polyline
        for (var i = 0; i < count; i++) {
          if (ret.steps[i].mode == 'WALKING' && ret.steps[i].polyline) {
            coors.push(ret.steps[i].polyline);
          }
          if (ret.steps[i].mode == 'TRANSIT' && ret.steps[i].lines[0].polyline) {
            coors.push(ret.steps[i].lines[0].polyline);
          }
        }
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 0; i < coors.length; i++) {
          for (var j = 2; j < coors[i].length; j++) {
            coors[i][j] = Number(coors[i][j - 2]) + Number(coors[i][j]) / kr;
          }
        }
        //定义新数组，将coors中的数组合并为一个数组

        var coorsArr = [];
        for (var i = 0; i < coors.length; i++) {
          coorsArr = coorsArr.concat(coors[i]);
        }
        //
        console.log("---------")
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coorsArr.length; i += 2) {
          pl.push({
            latitude: coorsArr[i],
            longitude: coorsArr[i + 1]
          })
        }
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        _this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  test() {
    console.log("调用地图")
    var that = this
    var name = that.data.name
    console.log("位置名:", name)
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        wx.openLocation({
          // 上述方法 2 中获取到的经纬度
          latitude: that.data.latitude,
          longitude: that.data.longitude,
          scale: 28,
          name: name, // 打开后显示的从WXML中传过来的地址名称
        })
      },
      fail(err) {
        console.log("失败！")
      }
    })
  },
})